package com.bis.ymyy.service.impl;

import com.bis.ymyy.common.exception.BusinessException;
import com.bis.ymyy.common.exception.EnumCode;
import com.bis.ymyy.common.utils.AddUserInfo;
import com.bis.ymyy.entity.Doctor;
import com.bis.ymyy.mapper.DoctorMapper;
import com.bis.ymyy.mapper.UserMapper;
import com.bis.ymyy.entity.User;
import com.bis.ymyy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DoctorMapper doctorMapper;

    @Override
    public List<User> getAllUser() {
        return userMapper.getAllUser();
    }

    @Override
    public User findUserById(int userId) {
        return userMapper.findUserById(userId);
    }

    @Override
    public User userLogin(String userName, String passWord) {
        User user = new User();
        if (userMapper.findUserByName(userName) == null) {
            Doctor doctor = doctorMapper.findByUserName(userName);
            if (doctor == null) {
                log.info("用户名不存在");
                throw new BusinessException(EnumCode.E_100116);
            } else {
                if (doctor.getPassword().equals(passWord)) {
                    BeanUtils.copyProperties(doctor, user);
                    user.setUserId(doctor.getId());
                } else {
                    log.info("密码错误");
                    throw new BusinessException(EnumCode.E_100115);
                }
            }
        } else {
            user = userMapper.findUserByName(userName);
            if(!user.getPassword().equals(passWord)){
                log.info("密码错误");
                throw new BusinessException(EnumCode.E_100115);
            }
        }
        return user;
    }

    @Override
    public User findUserByUserName(String userName) {
        return userMapper.findUserByName(userName);
    }

    @Override
    public int addUser(String userName, String password) {
        if (userMapper.findUserByName(userName) != null){
            log.info("添加失败，用户名已存在");
            throw new BusinessException(EnumCode.E_100114);
        }
        return userMapper.addUser(userName, password);
    }

    @Override
    public int deleteUserById(int userId) {
        if (findUserById(userId) == null) {
            log.info("删除失败，用户不存在");
            throw new BusinessException(EnumCode.E_100116);
        }
//        TODO
//        如果预约表里有该用户则不可删除

        return userMapper.deleteUserById(userId);
    }

    @Override
    public int updateUser(User user) {
        if (user == null){
            throw new BusinessException(EnumCode.E_100108);
        }
        if (findUserById(user.getUserId()) == null){
            throw new BusinessException(EnumCode.E_100116);
        }

        return userMapper.updateUser(user);
    }

    @Override
    public boolean addBaseUser(AddUserInfo userInfo) {
        if (userInfo == null){
            throw new BusinessException(EnumCode.E_100108);
        }
        return userMapper.addBaseUser(userInfo);
    }


}
